package com.tbb.app.base.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.tbb.app.base.dto.ChannelAddDTO;
import com.tbb.app.base.dto.ChannelUpdateDTO;
import com.tbb.app.base.dto.UserAddDTO;
import com.tbb.app.base.dto.UserUpdateDTO;
import com.tbb.app.base.search.*;
import com.tbb.app.base.service.*;
import com.tbb.app.base.vo.*;
import com.tbb.app.common.entity.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/api/lzs")
@Slf4j
@ApiSupport(order = 3)
@Api(tags = "刘中仕相关接口")
//@CrossOrigin(origins = "*")
public class LZSController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IChannelService iChannelService;
    @Autowired
    private IMessageService iMessageService;
    @Autowired
    private INoteService iNoteService;
    @Autowired
    private IUserNoteService iUserNoteService;
    @Autowired
    private IFileService iFileService;

    @PostMapping("/user/page")
    @ApiOperation(value = "分页查询用户")
    @ApiOperationSupport(order = 1)
    public BaseResponse<IPage<UserVO>> pageUser(
            @RequestBody
            @Validated
                    UserPageSearch search) {
        iUserService.checkAdmin();
        return BaseResponse.data(iUserService.page(search, false));
    }

    @PostMapping("/user/add")
    @ApiOperation(value = "新增用户")
    @ApiOperationSupport(order = 2)
    public BaseResponse<Object> addUser(
            @RequestBody
            @Validated
                    UserAddDTO dto) {
        iUserService.checkAdmin();
        iUserService.add(dto);
        return BaseResponse.ok();
    }

    @GetMapping("/user/reset")
    @ApiOperation(value = "用户重置密码")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Object> resetUser(
            @RequestParam
            @ApiParam(value = "用户id，必填", required = true)
                    String userId) {
        iUserService.checkAdmin();
        iUserService.reset(userId);
        return BaseResponse.ok();
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "编辑用户")
    @ApiOperationSupport(order = 4)
    public BaseResponse<Object> updateUser(
            @RequestBody
            @Validated
                    UserUpdateDTO dto) {
        iUserService.checkAdmin();
        iUserService.update(dto);
        return BaseResponse.ok();
    }

    @GetMapping("/user/admin")
    @ApiOperation(value = "用户管理授权")
    @ApiOperationSupport(order = 5)
    public BaseResponse<Object> adminUser(
            @RequestParam
            @ApiParam(value = "用户id，必填", required = true)
                    String userId,
            @RequestParam
            @ApiParam(value = "操作类型，必填，0-取消授权，1-管理授权", required = true)
                    int type) {
        iUserService.checkAdmin();
        iUserService.admin(userId, type);
        return BaseResponse.ok();
    }

    @PostMapping("/channel/page")
    @ApiOperation("分页查询频道")
    @ApiOperationSupport(order = 6)
    public BaseResponse<IPage<ChannelVO>> pageChannel(
            @RequestBody
                    ChannelPageSearch search) {
        iUserService.checkAdmin();
        return BaseResponse.data(iChannelService.page(search));
    }

    @PostMapping("/channel/add")
    @ApiOperation("新增频道")
    @ApiOperationSupport(order = 7)
    public BaseResponse<Object> addChannel(
            @RequestBody
            @Validated
                    ChannelAddDTO dto) {
        iUserService.checkAdmin();
        iChannelService.add(dto);
        return BaseResponse.ok();
    }

    @PostMapping("/channel/update")
    @ApiOperation("编辑频道")
    @ApiOperationSupport(order = 8)
    public BaseResponse<Object> updateChannel(
            @RequestBody
            @Validated
                    ChannelUpdateDTO dto) {
        iUserService.checkAdmin();
        iChannelService.update(dto);
        return BaseResponse.ok();
    }

    @PostMapping("/message/page")
    @ApiOperation("分页查询消息")
    @ApiOperationSupport(order = 9)
    public BaseResponse<IPage<MessageAdminVO>> pageMessage(
            @RequestBody
            @Validated
                    MessageAdminPageSearch search) {
        iUserService.checkAdmin();
        return BaseResponse.data(iMessageService.page(search));
    }

    @PostMapping("/count/base")
    @ApiOperation("查询用户、笔记、评论数")
    @ApiOperationSupport(order = 10)
    public BaseResponse<CountBaseVO> countBase() {
        iUserService.checkAdmin();
        CountBaseVO vo = new CountBaseVO();
        vo.setUserNum(iUserService.count());
        vo.setNoteNum(iNoteService.count());
        vo.setRemarkNum(iUserNoteService.countRemark());
        return BaseResponse.data(vo);
    }

    @GetMapping("/channel/list")
    @ApiOperation("列表查询频道")
    @ApiOperationSupport(order = 11)
    public BaseResponse<List<ChannelVO>> listChannel() {
        iUserService.checkAdmin();
        return BaseResponse.data(iChannelService.listVO());
    }

    @GetMapping("/note/heat")
    @ApiOperation(value = "根据频道查询热门笔记TOP5")
    @ApiOperationSupport(order = 12)
    public BaseResponse<List<NoteVO>> heatNote(
            @RequestParam(required = false)
            @ApiParam(value = "频道id，不填查所有")
                    String channelId) {
        iUserService.checkAdmin();
        return BaseResponse.data(iNoteService.listHeat(channelId, 5));
    }

    @GetMapping("/note/channel")
    @ApiOperation(value = "查询笔记分布")
    @ApiOperationSupport(order = 13)
    public BaseResponse<List<SimpleVO>> noteChannel() {
        iUserService.checkAdmin();
        return BaseResponse.data(iNoteService.noteChannel());
    }

    @GetMapping("/remark/channel")
    @ApiOperation(value = "查询评论分布")
    @ApiOperationSupport(order = 14)
    public BaseResponse<List<SimpleVO>> remarkChannel() {
        iUserService.checkAdmin();
        return BaseResponse.data(iUserNoteService.remarkChannel());
    }

    @PostMapping("/file/upload")
    @ApiOperation("文件上传")
    @ApiOperationSupport(order = 15)
    @ApiImplicitParam(name = "file", value = "单文件上传", required = true, dataType = "MultipartFile", allowMultiple = true, paramType = "form")
    public BaseResponse<String> uploadFile(
            @RequestPart("file")
            @NotNull(message = "文件不能为空")
                    MultipartFile file) {
        iUserService.checkAdmin();
        return BaseResponse.data(iFileService.upload(file));
    }

    @GetMapping("/user/detail")
    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 16)
    public BaseResponse<UserVO> detailUser(
            @RequestParam(required = false)
            @ApiParam(value = "用户id，不填则表示本人")
                    String userId) {
        iUserService.checkAdmin();
        return BaseResponse.data(iUserService.detail(userId));
    }

    @PostMapping("/note/page")
    @ApiOperation("分页查询笔记")
    @ApiOperationSupport(order = 17)
    public BaseResponse<IPage<NoteAdminVO>> pageNote(
            @RequestBody
            @Validated
                    NoteAdminPageSearch search) {
        iUserService.checkAdmin();
        return BaseResponse.data(iNoteService.page(search));
    }

    @GetMapping("/note/delete")
    @ApiOperation("删除笔记")
    @ApiOperationSupport(order = 18)
    public BaseResponse<Object> deleteNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String id) {
        iUserService.checkAdmin();
        iNoteService.removeNote(id);
        return BaseResponse.ok();
    }

    @GetMapping("/note/detail")
    @ApiOperation("笔记详情")
    @ApiOperationSupport(order = 19)
    public BaseResponse<NoteAdminDetailVO> detailNote(
            @RequestParam
            @ApiParam(value = "笔记id", required = true)
                    String id) {
        iUserService.checkAdmin();
        return BaseResponse.data(iNoteService.detailNote(id));
    }

    @PostMapping("/note/remark/page")
    @ApiOperation("分页查询评论")
    @ApiOperationSupport(order = 20)
    public BaseResponse<IPage<RemarkVO>> pageRemark(
            @RequestBody
            @Validated
                    RemarkPageSearch search) {
        iUserService.checkAdmin();
        return BaseResponse.data(iUserNoteService.pageRemark(search));
    }

    @GetMapping("/note/remark/delete")
    @ApiOperation("删除评论")
    @ApiOperationSupport(order = 21)
    public BaseResponse<Object> deleteRemark(
            @RequestParam
            @ApiParam(value = "评论id", required = true)
                    String id) {
        iUserService.checkAdmin();
        iUserNoteService.removeRemark(id);
        return BaseResponse.ok();
    }

}
